package com.example.restaurant.dao;

import com.example.restaurant.controller.form.ModifyDishNumberForm;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.model.Dish;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.restaurant.service.dto.SelDishTypeByUserId;
import com.example.restaurant.service.dto.SelStoreByUidDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import com.example.restaurant.service.dto.FeiDishDto;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜品表 Mapper 接口
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
public interface DishMapper extends BaseMapper<Dish> {
    @Update("update dish set stock=#{form.dishNumber},quantity=#{form.quantity} where id=#{form.dishId}")
    int modifyDishNumber(@Param("form") ModifyDishNumberForm form);
    @Select("SELECT d.id,dt.dish_type_name,d.dish_type_id,d.dish_name,d.`status`,d.dish_price,d.unit,d.dish_img,d.dish_describe,d.quantity,d.stock,d.store_id FROM `user` u JOIN user_store us  ON u.id=us.user_id JOIN store s ON s.id=us.store_id JOIN dish d ON d.store_id=s.id JOIN dish_type dt ON dt.id=d.dish_type_id where u.id=#{uid}")
    IPage<FeiDishDto> getDish(Page<FeiDishDto> page, @Param("uid") int userId);

    @Select("SELECT s.id,s.store_name  FROM `user` u JOIN user_store us ON u.id=us.user_id  JOIN store s ON s.id=us.store_id  WHERE u.id=#{uid}")
    List<SelStoreByUidDto> selStoreByUid(@Param("uid")int userId);

    @Select("select * from dish_type where user_id=#{uid}")
    List<SelDishTypeByUserId> selDishTypeByUserId(@Param("uid")int userId);

    @Select("<script>SELECT * FROM dish  d JOIN dish_type dt ON d.dish_type_id=dt.id   <where>\n" +
            "            <if test=\"dishName!=null||dishName!=''\">\n" +
            "                d.dish_Name LIKE #{dishName}\n" +
            "            </if>\n" +
            "\n" +
            "        </where>\n" +
            "        AND d.store_id=#{storeId}</script>")
    IPage<FeiDishDto> getDishByStoreAndName(Page<FeiDishDto> page,@Param("storeId") int storeId,@Param("dishName") String dishName);
}
